<?php

namespace app\cps\controller;

use app\cps\service\EchoService;
use think\facade\Request;
use app\common\Common;

// 回传
class Back extends Base
{
    private $service;

    public function __construct(EchoService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染回传设置列表页面
    public function index()
    {
        return view();
    }

    // 渲染回传设置详情页面
    public function info()
    {
        return view();
    }

    // 自定义回传设置列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getCustomReturnRuleList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 自定义回传设置详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Back::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getCustomReturnRuleDetail($params['id']);

        return Common::success($data);
    }

    // 添加自定义回传设置
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Back::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomReturnRule($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 修改自定义回传设置
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Back::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomReturnRule($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 删除自定义回传设置
    public function test()
    {
        $pass = 5;
        $leak = 2;

        $sum = $pass + $leak;

        for ($i = 1; $i <= 100; $i++) {
            if ($i % $sum >= $leak) {
                echo ' 第'.$i.'个用户的充值回传 ';
            } else {
                echo ' 第'.$i.'个用户的充值不回传 ';
            }

            if ($i % $sum == 0) {
                echo PHP_EOL;
            }
        }
        die;
    }

    // 获取自定义回传设置下拉列表
    public function select()
    {
        $params = Request::param();

        // try {
        //     validate(\app\cps\validate\Back::class)->scene('select')->check($params);
        // } catch (\Exception $e) {
        //     return Common::fail($e->getError());
        // }

        $data = $this->service->getCustomBackSetSelectList($this->getUserInfo());

        return Common::success($data);
    }

    // 修改回传率
    public function editEchoRate()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Back::class)->scene('edit_echo_rate')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editLinkEchoRate($params, $this->getUserInfo());

        if (!empty($result['message'])) {
            return Common::fail($result['message']);
        }

        return Common::success();
    }

    // 修改回传率显示回传数据
    public function showEchoData()
    {
        $params = Request::param();

        $data = $this->service->showLinkEchoData($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 渲染已回传数据列表页面
    public function alreadyReturn()
    {
        return view();
    }

    // 已回传数据列表
    public function alreadyEchoList()
    {
        $params = Request::param();

        $data = $this->service->getAlreadyEchoList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 渲染未回传数据列表页面
    public function notReturn()
    {
        return view();
    }

    // 未回传数据列表
    public function notEchoList()
    {
        $params = Request::param();

        $data = $this->service->getNotEchoList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 手动批量回传
    public function batchEcho()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Back::class)->scene('batch_echo')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->batchEcho($params, $this->getUserInfo());

        if ($result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染今日未支付用户手动回传页面
    public function unpaidUserManualReturn()
    {
        return view();
    }

    // 今日激活未充值用户列表
    public function notRechargeUserList()
    {
        $params = Request::param();
        $data = $this->service->getNotRechargeUserList($params, $this->getUserInfo());
        return Common::success($data);
    }

    // 未支付用户手动回传
    public function manualReturn()
    {
        $params = Request::param();

        $data = $this->service->unpaidUserManualReturn($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    // 渲染未充值用户回传记录列表页面
    public function manualReturnRecordIndex()
    {
        return view();
    }

    // 未支付用户手动回传记录
    public function manualReturnRecord()
    {
        $params = Request::param();
        $data = $this->service->unpaidUserManualReturnRecord($params, $this->getUserInfo());
        return Common::success($data);
    }

    // 批量修改回传设置
    public function batchUpdateSet()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('batch_update_back')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->batchUpdateBackSet($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    // 渲染批量设置页面
    public function batchSet()
    {
        return view();
    }
}